﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using Utility;

namespace Remind.View
{
    public partial class FrmFaceManager : Form
    {
        ImageList faceImgList = null;
        PictureBox picBox = null;
        int _faceid = 0;
        private string _path = Application.StartupPath + "\\face";
        public FrmFaceManager()
        {
            InitializeComponent();
        }
        
        public FrmFaceManager(ImageList imglist,int faceid)
        {
            InitializeComponent();
            faceImgList = imglist;
            _faceid = faceid;
            picBox.Image = faceImgList.Images[_faceid];
        }

        public FrmFaceManager(ImageList imglist, PictureBox picBoxFace)
        {
            InitializeComponent();
            faceImgList = imglist;
            picBox = picBoxFace;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //把头像给listview
            listView1.LargeImageList = faceImgList;
            
            //循环读取imagelist中的图像
            for (int i = 0; i < faceImgList.Images.Count; i++)
            {
                listView1.Items.Add(new ListViewItem() { ImageIndex = i });
            }
        }

        private bool ThumbnaiCallBack()
        {
            return false;
        }
        //创建缩略图
        private Bitmap CreateThumbnail(string source)
        {
            //source表示原图路径，dest参数表示缩略图路径
            Image.GetThumbnailImageAbort myCallBack = new Image.GetThumbnailImageAbort(ThumbnaiCallBack);
            Bitmap bmp = new Bitmap(source);
            int x = bmp.Width;
            int y = bmp.Height;
            try
            {
                if (x > 42 || y > 42)
                {
                    float scale = (x > y) ? (x / 42F) : (y / 42F);
                    Image aThumbnail = bmp.GetThumbnailImage((int)(x / scale), (int)(x / scale), myCallBack, IntPtr.Zero);
                    return (Bitmap)aThumbnail;
                }
                else
                {
                    return bmp;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                bmp.Dispose();
            }
        }
        
        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {

        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            _faceid = e.Item.ImageIndex;
            picBox.Image = faceImgList.Images[_faceid];
        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            frmContact.faceid = _faceid;
            FrmAddContact.faceid = _faceid;
            this.Close();
        }

        private void btnUpLoad_Click(object sender, EventArgs e)
        {
            //打开文件窗口
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                ArrayList names = new ArrayList();//存放目的文件夹中存在的文件。
                names.AddRange(Directory.GetFiles(_path));
                for (int i = 0; i < names.Count; i++)
                {
                    names[i] = Path.GetFileNameWithoutExtension((string)names[i]).ToUpper();
                }
                try
                {
                    foreach (string s in openFileDialog.FileNames)
                    {
                        if (PicInfo.IsImage(s))//选中的是图像
                        {
                            PicInfo picInfo = new PicInfo(s);
                            string name = InsertAName(picInfo.NameNoExtension, names);
                            string destFile = _path + "\\" + name + picInfo.GetExtension();
                            //File.Copy(picInfo.FullName, destFile);
                            CreateThumbnail(picInfo.FullName, destFile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                faceImgList.Images.Clear();
                foreach (string targetDir in Directory.GetFiles(_path))
                {
                    Image img = Image.FromFile(targetDir);
                    faceImgList.Images.Add(img);
                }
                listView1.Items.Clear();
                //循环读取imagelist中的图像
                for (int i = 0; i < faceImgList.Images.Count; i++)
                {
                    listView1.Items.Add(new ListViewItem() { ImageIndex = i });
                }
            }
        }
        //创建缩略图
        private void CreateThumbnail(string source, string dest)
        {
            //source表示原图路径，dest参数表示缩略图路径
            Image.GetThumbnailImageAbort myCallBack = new Image.GetThumbnailImageAbort(ThumbnaiCallBack);
            Bitmap bmp = new Bitmap(source);
            int x = bmp.Width;
            int y = bmp.Height;
            try
            {
                if (x > 64 || y > 64)
                {
                    float scale = (x > y) ? (x / 64F) : (y / 64F);
                    Image aThumbnail = bmp.GetThumbnailImage((int)(x / scale), (int)(x / scale), myCallBack, IntPtr.Zero);
                    aThumbnail.Save(dest);
                }
                else
                {
                    bmp.Save(dest);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                bmp.Dispose();
            }
        }

        private string InsertAName(string aName, ArrayList names)
        {
            int nameExtend = 0;
            string tempName = aName;//新图片名称
            int namesCount = names.Count;//目标目录存在的文件数量
            for (int i = 0; i < namesCount; i++)
            {
                string name = (string)names[i];//目标文件名
                if (tempName.ToUpper().CompareTo(name) == 0)//复制近来的文件和目标名比较
                {
                    nameExtend++;//
                    tempName = System.IO.Path.GetFileNameWithoutExtension(aName) + "-" + nameExtend.ToString();//改名
                }
                if (tempName.ToUpper().CompareTo(name) == -1)
                {
                    names.Insert(i, tempName);
                    break;
                }
                if (i == namesCount - 1)
                {
                    names.Add(tempName);
                }
            }
            return tempName;
        }
    }
}
